<template>
  <div class="edit-popup">
    <popup
        ref="popupRef"
        :title="popupTitle"
        :async="true"
        width="550px"
        :clickModalClose="true"
        @confirm="handleSubmit"
        @close="handleClose"
    >
      <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules">
        <el-form-item label="名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="比赛方式" prop="playType">
          <el-select class="flex-1" v-model="formData.playType" placeholder="请选择比赛方式">
            <el-option label="男单打" :value=1 />
            <el-option label="女单打" :value=2 />
            <el-option label="男双打" :value=3 />
            <el-option label="女双打" :value=4 />
            <el-option label="男女混打" :value=5 />
          </el-select>
        </el-form-item>
        <el-form-item label="赛程" prop="schedule">
          <el-select class="flex-1" v-model="formData.schedule" placeholder="请选择赛程">
            <el-option label="初赛" :value=1 />
            <el-option label="半决赛" :value=2 />
            <el-option label="总决赛" :value=3 />
          </el-select>
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {
  competitionSiteAdd, competitionSiteDetail,
  competitionSiteEdit
} from '@/api/competition/competition'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
  dictData: {
    type: Object as PropType<Record<string, any[]>>,
    default: () => ({})
  }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
  return mode.value == 'edit' ? '编辑赛事场地' : '新增赛事场地'
})
const route = useRoute()
const formData = reactive({
  id: '',
  name: '',
  address: '',
  playType: '',
  schedule: '',
  competitionId: route.query.id
})

const formRules = {
  id: [
    {
      required: true,
      message: '请输入',
      trigger: ['blur']
    }
  ],
  name: [
    {
      required: true,
      message: '请输入名称',
      trigger: ['blur']
    }
  ],
  address: [
    {
      required: true,
      message: '请输入地点',
      trigger: ['blur']
    }
  ],
  playType: [
    {
      required: true,
      message: '请选择比赛方式：1:男单打，2:女单打，3:男双打，4:女双打，5:男女混打',
      trigger: ['blur']
    }
  ],
  schedule: [
    {
      required: true,
      message: '请输入赛程：初赛，半决赛，决赛',
      trigger: ['blur']
    }
  ]
}

const handleSubmit = async () => {
  await formRef.value?.validate()
  const data: any = { ...formData }
  mode.value == 'edit' ? await competitionSiteEdit(data) : await competitionSiteAdd(data)
  popupRef.value?.close()
  feedback.msgSuccess('操作成功')
  emit('success')
}

const open = (type = 'add') => {
  mode.value = type
  popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key]
    }
  }
}

const getDetail = async (row: Record<string, any>) => {
  const data = await competitionSiteDetail({
    id: row.id
  })
  setFormData(data)
}

const handleClose = () => {
  emit('close')
}

defineExpose({
  open,
  setFormData,
  getDetail
})
</script>
